package cn.xiaoming.car.server.content.controller;

import cn.xiaoming.car.pojo.car.pojo.param.CarAddNewParam;
import cn.xiaoming.car.pojo.car.pojo.param.CarUpdateInfoParam;
import cn.xiaoming.car.pojo.car.pojo.vo.CarListItemVO;
import cn.xiaoming.car.pojo.car.pojo.vo.CarVO;
import cn.xiaoming.car.server.ICarService;

import cn.xiaoming.car.server.jsonresult.JsonResult;
import cn.xiaoming.car.server.pojo.vo.PageData;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author Anan
 */
@Slf4j
@RestController
@RequestMapping("/content/cars")
@Validated
public class CarController {

    @Autowired
    ICarService carService;

    public CarController(){log.debug("创建控制类对象CarController");}

    @PostMapping("/add-new")
    public JsonResult addNew(@Validated CarAddNewParam tagAddNewParam) {
        log.debug("开始处理【发布车源】的请求，参数：{}", tagAddNewParam);
        carService.addNew(tagAddNewParam);
        return JsonResult.ok();
    }
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult delete(@PathVariable @Range(min = 1, message = "下架车辆失败，请提交合法的ID值！") Long id) {
        log.debug("开始处理【下架车辆】的请求，参数：{}", id);
        carService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/enable")
    public JsonResult setEnable(@PathVariable @Range(min = 1, message = "启用对应车辆信息失败，请提交合法的ID值！") Long id) {
        log.debug("开始处理【启用车辆信息】的请求，参数：{}", id);
        carService.setEnable(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/disable")
    public JsonResult setDisable(@PathVariable @Range(min = 1, message = "禁用标签失败，请提交合法的ID值！") Long id) {
        log.debug("开始处理【禁用车辆信息】的请求，参数：{}", id);
        carService.setDisable(id);
        return JsonResult.ok();
    }
    @GetMapping("/car/{id:[0-9]+}")
    public JsonResult getCarById(@PathVariable @Range(min = 1, message = "获取标签详情失败，请提交合法的ID值！")Long id){
        log.debug("开始处理【根据ID查询车辆】的请求，参数：{}", id);
        CarListItemVO car =carService.getCarById(id);
        return JsonResult.ok(car);
    }

    @GetMapping("/detail/{id:[0-9]+}")
    public JsonResult getDetailById(@PathVariable @Range(min = 1, message = "获取标签详情失败，请提交合法的ID值！")Long id){
        log.debug("开始处理【根据ID查询车辆】的请求，参数：{}", id);
        CarVO car =carService.getDetailById(id);
        return JsonResult.ok(car);
    }

    @PostMapping("/{id:[0-9]+}/update/info")
    public JsonResult updateInfoById(@Validated CarUpdateInfoParam carUpdateInfoParam) {
        log.debug("开始处理【根据ID修改车辆信息】的请求，参数：{}", carUpdateInfoParam);
        carService.updateInfoById(carUpdateInfoParam);
        return JsonResult.ok();
    }

    @GetMapping("")
    public JsonResult list(Integer page, String queryType) {
        log.debug("开始处理【查询车辆列表】请求，页码：{}", page);
        if (page == null) {
            page = 1;
        }
        Integer pageNum = page > 0 ? page : 1;
        PageData<CarListItemVO> pageData ;
        if ("all".equals(queryType)) {
            pageData = carService.list(1, Integer.MAX_VALUE);
        } else {
            pageData = carService.list(pageNum);
        }
        return JsonResult.ok(pageData);
    }

}
